package com.agile.leetcode.reflex;

//import jdk.nashorn.internal.ir.debug.ObjectSizeCalculator;
import sun.misc.Unsafe;

import java.lang.reflect.*;

/**
 * 反射的获取
 * @Author ChenZhangKun
 * @Date 2020/9/7 9:08
 */
public class ReflexDemo {
    public static void main(String[] args) throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Field field = Person.class.getDeclaredField("student");
        // 拿到权限类名
        Type genericType = field.getGenericType();
        String typeName = genericType.getTypeName();
        System.out.println(typeName);
        // 修改修饰符获取对象0
        Constructor<Student> constructor = Student.class.getDeclaredConstructor();
        constructor.setAccessible(true);
        Student student = constructor.newInstance();
        student.setName("czj");
        student.setAge(28);
        System.out.println(student);
        // 反射获取对象
        Class<?> aClass = Class.forName(typeName);
        aClass.getDeclaredConstructor().setAccessible(true);
        Student o = (Student) aClass.newInstance();
        o.setAge(25);
        o.setName("czk");
        System.out.println(o);

    }
}
class Person{
    private Student student;
    private int num;

    public Person(Student student, int num) {
        this.student = student;
        this.num = num;
    }

    @Override
    public String toString() {
        return "Person{" +
                "student=" + student +
                ", num=" + num +
                '}';
    }
}
class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    private Student(){}

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Student student=new Student("nzq",25);
      //  long objectSize = ObjectSizeCalculator.getObjectSize(student);
        long objectSize = 0L;
        System.out.println(objectSize);
        Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
        // 忽略权限检查
        theUnsafe.setAccessible(true);
        Unsafe unsafe=(Unsafe)theUnsafe.get(null);
        Field[] declaredFields = Student.class.getDeclaredFields();
        // 获取偏移地址
        long total=0L;
        for (Field declaredField : declaredFields) {
            total=total+unsafe.objectFieldOffset(declaredField);
            System.out.println(declaredField.getName()+"  "+unsafe.objectFieldOffset(declaredField));
        }
        System.out.println(total);
    }
}
